﻿using System;
using System.Reflection;

namespace Draco.OWF.RT.Engine.Manager
{
    public class AssemblyLoader
    {
#if NETCORE
        public static Assembly LoadAssembly(String basePath, String assemblyName)
        {
            var ctx = new PluginAssemblyLoadContext() { AssemblyDirectory= basePath };
            return ctx.LoadFromAssemblyName(new AssemblyName(assemblyName)); 
        }

        public class PluginAssemblyLoadContext : System.Runtime.Loader.AssemblyLoadContext
        {
            public String AssemblyDirectory { get; set; }
            protected override Assembly Load(AssemblyName assemblyName)
            {
                String fullPath = AssemblyDirectory + assemblyName.Name + ".dll";
                return base.LoadFromAssemblyPath(fullPath);
            }
        }

#else
        public static Assembly LoadAssembly(String basePath, String assemblyName)
        {
            AssemblyName aName = new AssemblyName(assemblyName);
            String fullPath = basePath + aName.Name + ".dll";
            return Assembly.LoadFrom(fullPath);
        }
#endif
    }
}
